package com.situ.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.pojo.entity.Admin;
import com.situ.mall.mapper.AdminMapper;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2025-09-10
 */
@Transactional
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    private static final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);

    @Autowired
    private AdminMapper adminMapper;

    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT)
    @Override
    public Set<String> selectAllImage() {
        //开始时间  关注点代码
        //long begin = System.currentTimeMillis();

        //业务代码
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("avatar");
        queryWrapper.isNotNull("avatar");
        List<Admin> list = adminMapper.selectList(queryWrapper);
        Set<String> set = new HashSet<>();
        for (Admin admin : list) {
            set.add(admin.getAvatar());
        }

        //记录结束时间   关注点代码
        /*long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000) {
            logger.error("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else if (takeTime > 2000) {
            logger.warn("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else {
            logger.info("=====执行结束，耗费 {}毫秒=======", takeTime);
        }*/

        return set;
    }

    @Override
    public IPage<Admin> list(AdminQuery adminQuery) {
        //开始时间
        //long begin = System.currentTimeMillis();

        IPage<Admin> page  = new Page<>(adminQuery.getPage(), adminQuery.getLimit());

        //记录结束时间
        /*long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000) {
            logger.error("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else if (takeTime > 2000) {
            logger.warn("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else {
            logger.info("=====执行结束，耗费 {}毫秒=======", takeTime);
        }*/

        return adminMapper.list(page, adminQuery);
    }

}
